import * as userService from '../services/users';

export default {
  namespace: 'userList',
  state: {
    data: [],
    total: 0,
    defaultPageSize: 15,
    selectedRowKeys: [],
  },
  reducers: {
    save(state, { payload: data }) {
      return { ...state, data: data.list, total: data.total, defaultPageSize: data.pageSize };
    },
  },
  effects: {
    *fetch({ payload: { page = 1 } }, { call, put }) {
      const data = yield call(userService.fetch, { page });
      yield put({ type: 'save', payload: data });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/User') {
          dispatch({
            type: 'fetch',
            payload: query,
          });
        }
      });
    },
  },
};
